//
//  MineCotentListView.m
//  WeCommunity
//
//  Created by mac on 2017/6/15.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "MineCotentListView.h"



@interface MineCotentListView ()<UITableViewDelegate,UITableViewDataSource>


@end

@implementation MineCotentListView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self addSubview:self.tableView];
        
    }
    return self;
}

-(void)reloadWithData
{
    self.listArray = [NSMutableArray array];
    
    //用户数据
//    [self.listArray addObject:@"0"];
    [self.listArray addObject:self.contact];
    [self.listArray addObject:@"1"]; //加一些其他数据进去;
    
    //更多选项
    NSArray * imageArr = @[@"write",@"coin",@"mineContent",@"like",@"rule"];
    NSArray * nameArr = @[@"sign in",@"coin",@"have published",@"like",@"regulation"];
    if([UserInfoManger shared].isLogin){
        imageArr = @[@"write",@"coin",@"mineContent",@"like",@"rule",@"logout"];
        nameArr = @[@"sign in",@"coin",@"have published",@"like",@"regulation",@"Logout"];
    }
    
    for (int i = 0 ; i < imageArr.count; i++) {
        MineContentListModel * model = [MineContentListModel new];
        model.contentImage = [UIImage imageNamed:imageArr[i]];
        model.contentName = HH_LOC_STR(nameArr[i]);
        
        
        if(i==1 && [UserInfoManger shared].isLogin){
            
            if(IS_CHINESE){
                model.detail = [NSString stringWithFormat:@"%@ 积分",[UserInfoManger shared].contact.coins];
            }else{
                model.detail = [NSString stringWithFormat:@"%@ coins",[UserInfoManger shared].contact.coins];
            }
        }
        
        [self.listArray addObject:model];
    }
    
    //管理员权限
    if ([UserInfoManger shared].userIsAdmin) {
        [self.listArray addObject:@"1"]; //加一些其他数据进去;
        MineContentListModel * model = [MineContentListModel new];
        model.contentImage = [UIImage imageNamed:imageArr[0]];
        model.contentName = @"收到的举报内容";
        [self.listArray addObject:model];
    }
    
    
    [self.tableView reloadData];
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor colorWithHex:0xf8f8f8 alpha:1.0];
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

#pragma mark - tableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id object = self.listArray[indexPath.row];
    
    if ([object isKindOfClass:[NSString class]]) {
        if ([(NSString *)object isEqualToString:@"0"]) {
            return NULL_ROW1_Height;
        }else if ([(NSString *)object isEqualToString:@"1"]){
            return NULL_ROW2_Height;
        }
    }else if ([object isKindOfClass:[ContactModel class]]) {
        return Row1_Height;
    }else if ([object isKindOfClass:[MineContentListModel class]]) {
        return Row2_Height;
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    MineListSpaceCell
    id object = self.listArray[indexPath.row];
    
    if ([object isKindOfClass:[NSString class]]) {
        //隔行cell
        
        static NSString * cellID = @"mineListSpaceCell";
        MineListSpaceCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell =[[MineListSpaceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        //没有点击效果
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }else if ([object isKindOfClass:[ContactModel class]]) {
        static NSString * cellID = @"mineList1Cell";
        MinePersonInfoCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell =[[MinePersonInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        
        [cell creatCellWithModel:object];
        [cell needAddTopLine:YES addBottomLine:YES];
        return cell;
    }else if ([object isKindOfClass:[MineContentListModel class]]) {
        static NSString * cellID = @"mineList2Cell";
        MineContentListCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell =[[MineContentListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        [cell creatCellWithModel:object];
        if (indexPath.row == 2) {
            [cell needAddTopLine:YES addBottomLine:NO];
        }else if(indexPath.row == self.listArray.count - 1){
            [cell needAddTopLine:NO addBottomLine:YES];
        }else{
            [cell needAddTopLine:NO addBottomLine:NO];
        }
        return cell;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    id object = self.listArray[indexPath.row];
    if ([object isKindOfClass:[NSString class]]) {
        return;
    }

    NSInteger index = indexPath.row;
    
    for (id obj in self.listArray){
        if(obj == object)
            break;
        
        if([obj isKindOfClass:[NSString class]]){
            index--;
        }
    }
    
    if ([self.delegate respondsToSelector:@selector(mineCotentListViewSelectRowAtIndex:)]) {
        [self.delegate mineCotentListViewSelectRowAtIndex:index];
    }

}


@end
